package com.jservice.web.smkBanner;

import com.jservice.framework.annotation.RouteBind;
import com.jservice.framework.dto.Message;
import com.jservice.framework.dto.Pager;
import com.jservice.model.SmkBanner;
import com.jservice.web.base.JBaseController;

/**
 * SmkBannerController - 轮播图
 *
 * @Author cwledit
 * @DATE 2017/7/10.
 */
@RouteBind(path = "/smk/banner")
public class SmkBannerController extends JBaseController {

    public void list(){
        Pager pager = SmkBannerService.me().findPage(getPageable());
        setAttr("pager",pager);
        render("list");
    }

    public void add(){
        render("add");
    }

    public void save(){

        SmkBanner banner = getModel(SmkBanner.class,"banner");

        if(SmkBannerService.me().save(banner)){
            setFlashMessage(Message.success("添加成功"));
        }else{
            setFlashMessage(Message.error("添加失败"));
        }
        redirect("/smk/banner/list");
    }

    public void edit(){
        String bannerId = getPara("id");
        SmkBanner banner = SmkBannerService.me().findById(bannerId);
        setAttr("banner",banner);
        render("edit");
    }

    public void update(){

        SmkBanner smkBanner = getModel(SmkBanner.class,"banner");
        if(SmkBannerService.me().update(smkBanner)){
            setFlashMessage(Message.success("修改成功"));
        }else{
            setFlashMessage(Message.error("修改失败"));
        }
        redirect("/smk/banner/list");
    }

    public void delete(){
        String[] ids = getParaValues("ids");

        if (ids != null) {
            for (String id : ids) {
                SmkBanner smkBanner = SmkBannerService.me().findById(id);
                if (smkBanner != null &&  SmkBanner.status.ONLINE.name().equals(smkBanner.getStatus()) ) {
                    renderJson(Message.error("存在上线状态的轮播广告,请先进行下线操作再删除!"));
                    return;
                }
            }
            for(String id : ids){
                SmkBannerService.DAO.deleteById(id);
            }
        }
        renderJson(Message.success("删除成功"));
    }

}
